package com.cencat.controller;

import com.cencat.domain.Student;
import com.cencat.serice.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/student")
public class controller {
    @Autowired
    private StudentService studentService;
    /*查询所有*/
    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Student> students = studentService.getAll();
        for (Student student : students) {
            System.out.println(student);
        }
        model.addAttribute("students",students);
        model.addAttribute("successMessage", "查询成功！");
        return "student";
    }

    /*通过id查询*/
    @RequestMapping("/findById")
    public String findById(Integer id,Model model){
        Student student = studentService.getStudentById(id);
        List<Student> students = new ArrayList<>();
        students.add(student);
        model.addAttribute("students",students);
        model.addAttribute("successMessageByID", "查询成功！");
        return "student";
    }

    /*新增*/
    @RequestMapping("/add")
    public String add(Student student, Model model) {
        studentService.save(student);
        model.addAttribute("insertSuccess", "新增成功！");
        return "student";
    }
    /*修改*/
    @RequestMapping("/updateById")
    public String updateById(Student student, Model model) {
        studentService.update(student);
        model.addAttribute("updateSuccess", "修改成功！");

        return "student";
    }

    /*通过id删除*/
    @RequestMapping("/deleteById")
    public String deleteById(int id,Model model) {
        studentService.delete(id);
        model.addAttribute("deleteByIdSuccess", "删除成功！");
        return "student";
    }

    /*分页查询*/
    @RequestMapping("/findByPage")
    public String findByPage(int page, int size, Model model){
        List<Student> students = studentService.getStudentsBypage(page,size);
        model.addAttribute("students",students);
        model.addAttribute("successMessage", "查询成功！");
        return "student";
    }


}
